<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">学生添加</div>
      <el-form
        status-icon
        :model="addForm"
        :rules="rules"
        ref="ruleForm"
        class="revise-form"
      >
        账号
        <el-form-item prop="stuNumber" >
          <el-input
            v-model="addForm.stuNumber"
            prefix-icon="el-icon-user-solid"
            placeholder="账号"
            @keyup.enter.native="add"
          />
        </el-form-item>
        姓名
        <el-form-item prop="stuName">
          <el-input
            v-model="addForm.stuName"
            prefix-icon="el-icon-user-solid"
            placeholder="姓名"
            @keyup.enter.native="add"
          />
        </el-form-item>
        <el-form-item label="性别" prop="stuSex">
          <el-radio v-model="addForm.stuSex" :label="sex.man" @keyup.enter.native="add">男</el-radio>
          <el-radio v-model="addForm.stuSex" :label="sex.human" @keyup.enter.native="add">女</el-radio>
        </el-form-item>
        密码
        <el-form-item prop="stuPassword">
          <el-input
            v-model="addForm.stuPassword"
            prefix-icon="el-icon-user-solid"
            show-password
            placeholder="密码"
            @keyup.enter.native="add"
          />
        </el-form-item>
        <el-form-item label="是否启用" prop="stuStatus">
          <el-radio v-model="addForm.stuStatus" :label=1 @keyup.enter.native="add">是</el-radio>
          <el-radio v-model="addForm.stuStatus" :label=0 @keyup.enter.native="add">否</el-radio>
        </el-form-item>
      </el-form>
      <!-- 登录按钮 -->
      <el-button type="primary" @click="add">添加</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "StudentAdd",

  data: function() {
    return {
      addForm: {
        stuNumber: "",
        stuName: "",
        stuPassword:"",
        stuStatus:"",
        stuSex:"",
      },
      rules: {
        stuNumber: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        stuName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        stuPassword: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        stuStatus: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        stuSex: [{ required: true, message: "性别不能为空", trigger: "blur" }]
      },
      sex:{
        man:"男",
        human:"女"
      }
    };
  },
  methods: {
    add() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const that = this;

          let param = new URLSearchParams();
          param.append("stuNumber", that.addForm.stuNumber);
          param.append("stuName", that.addForm.stuName);
          param.append("stuPassword", that.addForm.stuPassword);
          param.append("stuStatus", that.addForm.stuStatus);
          param.append("stuSex", that.addForm.stuSex);
          that.axios.post("/api/submit_stu_add", param).then(({ data }) => {
            if (data.flag) {
              that.$message.success("添加成功");
              that.$router.push({ path: "/" });
            } else {
              that.$message.error(data.message);
            }
          });


        } else {
          return false;
        }
      });
    }

  }
}
</script>

<style scoped>

</style>
